package cn.jinjimi.domain.data.imp;

import cn.jinjimi.app.controller.event.TaskListEvent;
import cn.jinjimi.domain.data.Store;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.AppService;
import cn.jinjimi.domain.data.net.TaskService;
import cn.jinjimi.domain.data.dto.TaskRecordDTO;
import cn.jinjimi.domain.store.TaskStore;
import cn.jinjimi.app.entity.TaskListBean;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * Created by zhenhua on 16/8/1.
 */
public class TaskStoreImp extends Store implements TaskStore {

    @Override
    public List<TaskListBean.TaskBean> getTaskList() throws IOException {
        TaskListBean dto = parseDataAndDispatchEvents(API.create(TaskService.class).getTasks().execute(),
                new TaskListEvent());

        return dto == null ? null : dto.getTasks();
    }

    @Override
    public boolean recordTask(String ssoid, int actionType) throws IOException {
        TaskService service = API.create(TaskService.class);
        Response<ResponseBody> response = service.record(new TaskRecordDTO(ssoid, actionType)).execute();
        return response.isSuccessful();
    }

    @Override
    public boolean recordShare(String ssoid, String url, String platform) throws IOException {
        AppService service = API.create(AppService.class);
        Response<ResponseBody> response = service.recordShare(ssoid, url, platform).execute();
        return response.isSuccessful();
    }
}
